unit UPesquisaNotaFiscalVenda;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFormPesquisa, Mask, ToolEdit, DBCtrls, StdCtrls, DB, DBTables,
  Grids, DBGrids, Buttons, ExtCtrls;

type
  TFPesquisaNotaFiscalVenda = class(TFormPesquisa)
    Label1: TLabel;
    edNNotaFiscal: TEdit;
    Label2: TLabel;
    edNPedido: TEdit;
    dblCliente: TDBLookupComboBox;
    Label4: TLabel;
    dtLanctoIni: TDateEdit;
    dtLanctoFim: TDateEdit;
    Label5: TLabel;
    dsCliente: TDataSource;
    QPesquisaNF_CODIGO: TIntegerField;
    QPesquisaNF_SITUACAO: TIntegerField;
    QPesquisaNF_NUMERONF: TIntegerField;
    QPesquisaNF_SERIENF: TStringField;
    QPesquisaNF_NUMEROSELO: TIntegerField;
    QPesquisaNF_SERIESELO: TStringField;
    QPesquisaNF_DATAEMISSAO: TDateTimeField;
    QPesquisaNF_TIPOVENDA: TIntegerField;
    QPesquisaNF_TIPO: TIntegerField;
    QPesquisaNF_PEDIDOVENDA: TIntegerField;
    QPesquisaNF_ROMANEIO: TIntegerField;
    QPesquisaNF_CLIENTE: TIntegerField;
    QPesquisaNF_VENDEDOR: TIntegerField;
    QPesquisaNF_PERCCOMISSAO: TFloatField;
    QPesquisaNF_CFOPNF: TIntegerField;
    QPesquisaNF_VALORSERVICO: TFloatField;
    QPesquisaNF_VALORDESCONTOSERVICO: TFloatField;
    QPesquisaNF_VALORFINALSERVICO: TFloatField;
    QPesquisaNF_VALORPRODUTO: TFloatField;
    QPesquisaNF_VALORDESCONTOPRODUTO: TFloatField;
    QPesquisaNF_VALORFINALPRODUTO: TFloatField;
    QPesquisaNF_PERCISS: TFloatField;
    QPesquisaNF_PERCIRRF: TFloatField;
    QPesquisaNF_PERCINSS: TFloatField;
    QPesquisaNF_VALORFRETE: TFloatField;
    QPesquisaNF_VALORSEGURO: TFloatField;
    QPesquisaNF_VALOROUTROS: TFloatField;
    QPesquisaNF_VALORIPI: TFloatField;
    QPesquisaNF_VALORFINALNOTA: TFloatField;
    QPesquisaNF_BASEICMSNORM: TFloatField;
    QPesquisaNF_VALORICMSNORM: TFloatField;
    QPesquisaNF_BASEICMSSUB: TFloatField;
    QPesquisaNF_VALORICMSSUB: TFloatField;
    QPesquisaNF_TRANSPORTADORA: TIntegerField;
    QPesquisaNF_FRETE: TIntegerField;
    QPesquisaNF_PLACA: TStringField;
    QPesquisaNF_PLACAUF: TStringField;
    QPesquisaNF_QUANTIDADEVOLUME: TStringField;
    QPesquisaNF_ESPECIEVOLUME: TStringField;
    QPesquisaNF_MARCA: TStringField;
    QPesquisaNF_NUMERO: TStringField;
    QPesquisaNF_PESOBRUTO: TFloatField;
    QPesquisaNF_PESOLIQUIDO: TFloatField;
    QPesquisaNF_DADOSADICONAIS: TStringField;
    QPesquisaNF_TEXTOLEGAL: TStringField;
    QPesquisaUSUARIO: TStringField;
    QPesquisaDATAMODIFICACAO: TDateTimeField;
    QPesquisaCODIGOEMPRESA: TIntegerField;
    QPesquisaNF_CANCELAMENTO: TStringField;
    QPesquisaNF_PERCPIS: TFloatField;
    QPesquisaNF_PERCCOFINS: TFloatField;
    QPesquisaNF_PERCCSLL: TFloatField;
    QPesquisaNF_DATAVENCIMENTO: TDateTimeField;
    QPesquisaNF_RETEMISS: TSmallintField;
    QPesquisaNF_OBS: TStringField;
    QPesquisaNF_ABATERETENCAO: TSmallintField;
    QPesquisaNF_FORMAPAGAMENTO: TStringField;
    QPesquisaNF_VALORNOTAFISCAL: TFloatField;
    QPesquisaNF_VALORIRRF: TFloatField;
    QPesquisaNF_VALORPIS: TFloatField;
    QPesquisaNF_VALORCOFINS: TFloatField;
    QPesquisaNF_VALORCSLL: TFloatField;
    QPesquisaNF_VALORINSS: TFloatField;
    QPesquisaNF_VALORISS: TFloatField;
    QPesquisaNF_RETENCAO: TFloatField;
    QPesquisaNF_COBRANCAPROGRAMADA: TIntegerField;
    QPesquisaNF_MESANOCOBRANCAPROGRAMADA: TStringField;
    QPesquisaNF_OBS1: TStringField;
    QPesquisaNF_OBS2: TStringField;
    QPesquisaNF_OBS3: TStringField;
    QPesquisaNF_OBS4: TStringField;
    QPesquisaNF_OBS5: TStringField;
    QPesquisaNF_VALORNOTAFISCAL2: TFloatField;
    QPesquisaNF_VALORNOTAFISCAL3: TFloatField;
    QPesquisaNF_DATAVENCIMENTO2: TDateTimeField;
    QPesquisaNF_DATAVENCIMENTO3: TDateTimeField;
    QPesquisaNF_VALORNOTAFISCAL1: TFloatField;
    QPesquisaNF_DATAVENCIMENTO1: TDateTimeField;
    QPesquisaNF_TEXTOLEGAL2: TStringField;
    BtnEtiqueta: TBitBtn;
    Label6: TLabel;
    procedure BtnConsultarClick(Sender: TObject);
    procedure dblClienteKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnLimparClick(Sender: TObject);
    procedure BtnSelecionarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnEtiquetaClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPesquisaNotaFiscalVenda: TFPesquisaNotaFiscalVenda;
  str_Cliente : String;

implementation

uses UDmGestao, UNotaFiscalMercadoriaServico, URelatorioEmissaoEtiqueta,
  UPesquisaRapida, UGeral;

{$R *.dfm}

procedure TFPesquisaNotaFiscalVenda.BtnConsultarClick(Sender: TObject);
const
  ctNotaFiscalVenda =
    'SELECT * FROM NOTAFISCALVENDA WHERE (NF_CODIGO > 0) %s';
var
  filtro : string;
begin
  if edNNotaFiscal.Text <> '' then
    filtro := 'AND (NF_NUMERONF = '+ edNNotaFiscal.Text +')';

  if edNPedido.Text <> '' then
    filtro := filtro + 'AND (NF_PEDIDOVENDA = '+ edNPedido.Text +')';

  if dblCliente.KeyValue <> null then
    filtro := filtro + 'AND (NF_CLIENTE = '+ varToStr(dblCliente.KeyValue) +')';

  if dtLanctoIni.Date <> 0 then
    filtro := filtro + 'AND (NF_DATAEMISSAO >= '''+ FormatDateTime('mm/dd/yyyy',dtLanctoIni.Date) +''')';

  if dtLanctoFim.Date <> 0 then
    filtro := filtro + 'AND (NF_DATAEMISSAO <= '''+ FormatDateTime('mm/dd/yyyy',dtLanctoFim.Date) +''')';

  QPesquisa.Close;
  QPesquisa.SQL.Clear;
  QPesquisa.SQL.Add(Format(ctNotaFiscalVenda,[filtro]));
  QPesquisa.Open;

  BtnEtiqueta.Enabled := not(QPesquisa.IsEmpty);

  inherited;

end;

procedure TFPesquisaNotaFiscalVenda.dblClienteKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
IF KEY = VK_F8 THEN
  BEGIN

    Application.CreateForm(TFPesquisaRapida,FPesquisaRapida);

    FPesquisaRapida.tipo := 'CLIENTE';
    CaPtUrA_pEsQuIsA_rApIdA := '';

    FPesquisaRapida.ShowModal;

    if CaPtUrA_pEsQuIsA_rApIdA <> '' then
      TDBLookupComboBox(Sender).KeyValue := CaPtUrA_pEsQuIsA_rApIdA;

    CaPtUrA_pEsQuIsA_rApIdA := '';

    Exit;

  END;

 case key of
  116 : begin
           TDBLookupComboBox(Sender).ListSource.DataSet.Close;
           TDBLookupComboBox(Sender).ListSource.DataSet.Open;
          end ;
 end;


 if key = 46 then
   TDBLookupComboBox(Sender).KeyValue := null;

end;

procedure TFPesquisaNotaFiscalVenda.BtnLimparClick(Sender: TObject);
begin
  inherited;
  TDBLookupComboBox(sender).KeyValue := null;
  edNNotaFiscal.Clear;
  edNPedido.Clear;
  dtLanctoIni.Date := 0;
  dtLanctoFim.Date := 0;
  QPesquisa.Close;
  BtnEtiqueta.Enabled := false;
end;

procedure TFPesquisaNotaFiscalVenda.BtnSelecionarClick(Sender: TObject);
begin
  inherited;
  FNotaFiscalMercadoriaServico.QNotaFiscal.Locate('NF_CODIGO',QPesquisa.FieldByName('NF_CODIGO').AsInteger,[]);
  Close;
end;

procedure TFPesquisaNotaFiscalVenda.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  FPesquisaNotaFiscalVenda := nil;
end;

procedure TFPesquisaNotaFiscalVenda.BtnEtiquetaClick(Sender: TObject);
CONST ct_sql = ' SELECT '+#10+
               '        CLI_RAZAO AS RAZAOSOCIAL, '+#10+
               '        CLI_ENDERECOCORRESP AS ENDERECORESP, '+#10+
               '        CLI_BAIRROCORRESP AS BAIRRORESP, '+#10+
               '        CLI_CEPCORRESP AS CEPRESP, '+#10+
               '        CLI_CIDADECORRESP AS CIDADERESP, '+#10+
               '        CLI_ESTADOCORRESP AS UFRESP, '+#10+
               '        CLI_ENDERECOPEDIDO AS ENDERECOPEDI, '+#10+
               '        CLI_BAIRROPEDIDO AS BAIRROPEDI, '+#10+
               '        CLI_CEPPEDIDO AS CEPPEDI, '+#10+
               '        CLI_CIDADEPEDIDO AS CIDADEPEDI, '+#10+
               '        CLI_ESTADOPEDIDO AS UFPEDI, '+#10+
               '        CLI_IDENTENDCORRESP AS TIPOENDERECO '+#10+
               ' FROM CLIENTE WHERE (CLI_CODIGO IN ( %s ) ) '+#10;
begin
  inherited;
  str_Cliente := '';

  while not(QPesquisa.eof) do
  begin

    str_Cliente := str_Cliente + QPesquisa.Fieldbyname('NF_CLIENTE').AsString + ', ';
    QPesquisa.next;

  end;

  str_Cliente := str_Cliente + QPesquisa.Fieldbyname('NF_CLIENTE').AsString;

  str_Cliente := COPY(str_Cliente,1,LENGTH(str_Cliente) - 1);

  str_Cliente := FORMAT(ct_sql,[str_Cliente]);

  Application.CreateForm(tFRelatorioEmissaoEtiqueta,FRelatorioEmissaoEtiqueta);
  
  FRelatorioEmissaoEtiqueta.QRelatorio.CLOSE;
  FRelatorioEmissaoEtiqueta.QRelatorio.SQL.CLEAR;
  FRelatorioEmissaoEtiqueta.QRelatorio.SQL.ADD(str_Cliente);
  FRelatorioEmissaoEtiqueta.QRelatorio.OPEN;
  FRelatorioEmissaoEtiqueta.QuickRep1.Prepare;
  FRelatorioEmissaoEtiqueta.QuickRep1.Preview;

end;

end.
